package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.constant.SerialNumConstants;
import io.aircraft.common.page.PageData;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.common.utils.ConvertUtils;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dao.PurchaseDao;
import io.aircraft.modules.wms.dto.PurchaseDTO;
import io.aircraft.modules.wms.dto.PurchaseDetailDTO;
import io.aircraft.modules.wms.entity.stored.ProcPurchaseCancelEntity;
import io.aircraft.modules.wms.entity.PurchaseEntity;
import io.aircraft.modules.wms.entity.stored.ProcPurchaseDoneEntity;
import io.aircraft.modules.wms.service.PurchaseDetailService;
import io.aircraft.modules.wms.service.PurchaseService;
import io.aircraft.modules.wms.service.SerialNumberService;
import io.aircraft.modules.wms.service.StoredProcService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 入库订单
 *
 * @author cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-16
 */
@Service
public class PurchaseServiceImpl extends CrudServiceImpl<PurchaseDao, PurchaseEntity, PurchaseDTO> implements PurchaseService {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private SerialNumberService serialNumberService;
    @Autowired
    private StoredProcService storedProcService;

    @Override
    public PageData<PurchaseDTO> page(Map<String, Object> params) {
        //查询入库订单
        IPage<PurchaseEntity> page = baseDao.selectPage(
                getPage(params, "purchase_id", true),
                getWrapper(params)
        );
        //查找所有订单明细
        List<PurchaseDetailDTO> detailList = purchaseDetailService.getAllDetail();
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //查询订单明细项
        page.getRecords().forEach(entity ->
        {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
            entity.setPurchaseDetailList(purchaseDetailService.getDetailByPurchaseId(entity.getPurchaseId(), detailList));
        });
        return getPageData(page, PurchaseDTO.class);
    }

    @Override
    public QueryWrapper<PurchaseEntity> getWrapper(Map<String, Object> params) {
        String purchaseNo = (String) params.get("purchaseNo");
        String spuNo = (String) params.get("spuNo");
        String lotNo = (String) params.get("lotNo");
        String stationNo = (String) params.get("stationNo");
        String purchaseSt = (String) params.get("purchaseSt");

        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(purchaseNo), "purchase_no", purchaseNo);
        wrapper.eq(StringUtils.isNotBlank(purchaseSt), "purchase_st", purchaseSt);
        wrapper.like(StringUtils.isNotBlank(stationNo), "station_no", stationNo);
        wrapper.like(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.eq(StringUtils.isNotBlank(lotNo), "lot_no", lotNo);
        return wrapper;
    }

    @Override
    @Transactional
    public void save(PurchaseDTO dto) {
        //1.获取需求数量
        BigDecimal amount = dto.getAmount();
        //2.获取主单编号
        String purchaseNo = serialNumberService.getSerialNumberByCode(SerialNumConstants.PURCHASE_MODULE_CODE);
        //3.填充单据信息
        dto.setPurchaseNo(purchaseNo);
        //4.将数据转换为对象实体
        PurchaseEntity entity = ConvertUtils.sourceToTarget(dto, PurchaseEntity.class);
        //5.将主单写入数据库
        insert(entity);
        //6.根据amount生成明细信息
        List<PurchaseDetailDTO> dtoList = new ArrayList<>();
        PurchaseDetailDTO detailDTO = new PurchaseDetailDTO();
        detailDTO.setPurchaseId(entity.getPurchaseId());
        detailDTO.setSpuNo(entity.getSpuNo());
        detailDTO.setLotNo(entity.getLotNo());
        detailDTO.setPlanQnty(BigDecimal.valueOf(1L));
        for (int i = 0; i < amount.intValue(); i++) {
            dtoList.add(detailDTO);
        }
        purchaseDetailService.batchSave(dtoList);
    }

    @Override
    public PurchaseDTO getListByStation(String stationNo) {
        PurchaseEntity entity = baseDao.selectByStation(stationNo);
        if (entity == null) {
            return null;
        }
        return ConvertUtils.sourceToTarget(entity, PurchaseDTO.class);
    }

    /**
     * 单据取消
     */
    @Override
    public ProcResult purchaseCancel(Long id) {
        ProcPurchaseCancelEntity parameters = new ProcPurchaseCancelEntity();
        parameters.setPurchaseId(id);
        return storedProcService.purchaseCancelById(parameters);
    }

    @Override
    public ProcResult purchaseDone(Long id) {
        final ProcPurchaseDoneEntity parameters = new ProcPurchaseDoneEntity();
        parameters.setPurchaseId(id);
        return storedProcService.purchaseDoneById(parameters);
    }
}